/*=====================================================================================
 *       Filename:  limitSwitch.c
===================================================================================== */

#include "limitSwitch.h"
#include "stdio.h"                  // Device header
#include "rs232_interface.h"

#define NUM_SWITCHS 6

bool bSwitchPressed[NUM_SWITCHS];
bool bEventSent[NUM_SWITCHS];

#define X_Min_Pin GPIO_Pin_14
#define X_Min_Port GPIOG
#define X_Max_Pin GPIO_Pin_14
#define X_Max_Port GPIOG
#define Y_Min_Pin GPIO_Pin_14
#define Y_Min_Port GPIOG
#define Y_Max_Pin GPIO_Pin_14
#define Y_Max_Port GPIOG
#define Z_Min_Pin GPIO_Pin_14
#define Z_Min_Port GPIOG
#define Z_Max_Pin GPIO_Pin_14
#define Z_Max_Port GPIOG

EndStopST endstop;

bool LimitSwitch_Pressed(LimitSwitch sw)
{
    bool val;
    switch (sw)
    {
    case LS_XMin:
        val = GPIO_ReadInputDataBit(X_Min_Port, X_Min_Pin);
        break;
    case LS_XMax:
        val = GPIO_ReadInputDataBit(X_Max_Port, X_Max_Pin);
        break;
    case LS_YMin:
        val = GPIO_ReadInputDataBit(Y_Min_Port, Y_Min_Pin);
        break;
    case LS_YMax:
        val = GPIO_ReadInputDataBit(Y_Max_Port, Y_Max_Pin);
        break;
    case LS_ZMin:
        val = GPIO_ReadInputDataBit(Z_Min_Port, Z_Min_Pin);
        break;
    case LS_ZMax:
        val = GPIO_ReadInputDataBit(Z_Max_Port, Z_Max_Pin);
        break;
    default:
        DBG_WARN("Invalid switch number %d", sw);
        return false;
    }
    return !val;
}

void LimitSwitch_HwInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);
    GPIO_InitStructure.GPIO_Pin = X_Min_Pin;
    GPIO_Init(X_Min_Port, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = X_Max_Pin;
    GPIO_Init(X_Max_Port, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = Y_Min_Pin;
    GPIO_Init(Y_Min_Port, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = Y_Max_Pin;
    GPIO_Init(Y_Max_Port, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = Z_Min_Pin;
    GPIO_Init(Z_Min_Port, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = Z_Max_Pin;
    GPIO_Init(Z_Max_Port, &GPIO_InitStructure);
}


void checkHitEndstops()
{
    if (endstop.END_XMin || endstop.END_XMax ||
        endstop.END_YMin || endstop.END_YMax ||
        endstop.END_ZMin || endstop.END_ZMax)
    {

        DBG_WARN("MSG_ENDSTOPS_HIT");
        if (endstop.END_XMin || endstop.END_XMax)
        {
            // rf_printf(" X:", (float)endstops_trigsteps[X_AXIS] / axis_steps_per_unit[X_AXIS]);
        }
        if (endstop.END_YMin || endstop.END_YMax)
        {
            // rf_printf(" Y:", (float)endstops_trigsteps[Y_AXIS] / axis_steps_per_unit[Y_AXIS]);
        }
        if (endstop.END_ZMin || endstop.END_ZMax)
        {
            // rf_printf(" Z:", (float)endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS]);
        }

        endstop.END_XMin = false;
        endstop.END_XMax = false;
        endstop.END_YMin = false;
        endstop.END_YMax = false;
        endstop.END_ZMax = false;
        endstop.END_ZMin = false;
#if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
        if (abort_on_endstop_hit)
        {
            card.sdprinting = false;
            card.closefile();
            quickStop();
            setTargetHotend0(0);
            setTargetHotend1(0);
            setTargetHotend2(0);
        }
#endif
    }
}



